package vashion.azeroth.api.controller.member;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.contant.AddressErrorEnum;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.personal.pojo.PersonalAddress;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalAddressService;

@Controller
@RequestMapping("/member")
public class AddressController<T> extends BaseController {

	private final static Logger log = LoggerFactory.getLogger(AddressController.class);

	@Autowired
	private PersonalAddressService<T> addressService;

	@RequestMapping("addAddr.htm")
	@ResponseBody
	public ApiResult<PersonalAddress> addAddress(@ModelAttribute("addr") PersonalAddress addr) {

		ApiResult<PersonalAddress> result = new ApiResult<PersonalAddress>();
		try {
			if (null == addr) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"参数" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == addr.getMemberId() || StringUtils.isBlank(String.valueOf(addr.getMemberId()))) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"会员ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (StringUtils.isBlank(addr.getProvince())) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"省名" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (StringUtils.isBlank(addr.getCity())) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"市名" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (StringUtils.isBlank(addr.getArea())) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"区名" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (StringUtils.isBlank(addr.getAddressDetail())) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"详细地址" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (StringUtils.isBlank(addr.getPhone())) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"收货电话" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (StringUtils.isBlank(addr.getReceiveName())) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"收货人姓名" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			result = addressService.addAddr(addr);
		} catch (Exception e) {
			log.error("添加收货地址异常", e);
			return ApiResult.newErrorResult(AddressErrorEnum.ADDR_ADD_FAIL.getErrorCode(),
					AddressErrorEnum.ADDR_ADD_FAIL.getErrorMessage());
		}
		return result;
	}

	@RequestMapping("getAddrList.htm")
	@ResponseBody
	public ApiResult<PersonalAddress> getAddrList(@ModelAttribute("addr") PersonalAddress addr) {
		ApiResult<PersonalAddress> addrList = new ApiResult<PersonalAddress>();
		try {

			if (null == addr.getMemberId() || StringUtils.isBlank(String.valueOf(addr.getMemberId()))) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"会员ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			addrList = addressService.getAddrList(addr);
		} catch (Exception e) {
			log.error("获取收货地址异常", e);
			return ApiResult.newErrorResult(AddressErrorEnum.ADDR_GET_FAIL.getErrorCode(),
					AddressErrorEnum.ADDR_GET_FAIL.getErrorMessage());
		}
		return addrList;
	}

	@RequestMapping("updAddr.htm")
	@ResponseBody
	public ApiResult<PersonalAddress> updAddr(@ModelAttribute("addr") PersonalAddress addr) {
		ApiResult<PersonalAddress> addrList = new ApiResult<PersonalAddress>();
		try {

			if (null == addr.getAddressId() || StringUtils.isBlank(String.valueOf(addr.getAddressId()))) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"地址ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			addrList = addressService.updAddr(addr);
		} catch (Exception e) {
			log.error("更新收货地址异常", e);
			return ApiResult.newErrorResult(AddressErrorEnum.UPDATE_ADDR_FAIL.getErrorCode(),
					AddressErrorEnum.UPDATE_ADDR_FAIL.getErrorMessage());
		}
		return addrList;
	}

	@RequestMapping("removeAddrByAddressId.htm")
	@ResponseBody
	public ApiResult<PersonalAddress> removeAddrByAddressId(@ModelAttribute("addr") PersonalAddress addr) {
		ApiResult<PersonalAddress> result = new ApiResult<PersonalAddress>();

		try {

			if (null == addr.getAddressId() || StringUtils.isBlank(String.valueOf(addr.getAddressId()))) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"地址ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == addr.getMemberId() || StringUtils.isBlank(String.valueOf(addr.getMemberId()))) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"会员ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			result = addressService.removeAddrByAddressId(addr);
		} catch (Exception e) {
			log.error("删除收货地址异常", e);
			return ApiResult.newErrorResult(AddressErrorEnum.ADDR_DEL_FAIL.getErrorCode(),
					AddressErrorEnum.ADDR_DEL_FAIL.getErrorMessage());
		}
		return result;
	}

	/**
	 * 查询是否有默认收货地址接口
	 * 
	 * 
	 */

	@RequestMapping("queryDefaultAddress.htm")
	@ResponseBody
	public ApiResult<PersonalAddress> queryDefaultAddress(@ModelAttribute("addr") PersonalAddress addr) {
		ApiResult<PersonalAddress> result = new ApiResult<PersonalAddress>();

		try {

			if (null == addr.getMemberId() || StringUtils.isBlank(String.valueOf(addr.getMemberId()))) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(),
						"会员ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			result = addressService.queryDefaultAddress(addr);
		} catch (Exception e) {
			log.error("查询默认收货地址异常", e);
			return ApiResult.newErrorResult(AddressErrorEnum.ADDR_GET_FAIL.getErrorCode(),
					AddressErrorEnum.ADDR_DEL_FAIL.getErrorMessage());
		}
		return result;
	}

}
